package com.dubbo.lian.cms.modules.app.service.impl;
 import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.dubbo.lian.cms.modules.app.UserTarget;
import com.dubbo.lian.cms.modules.app.mapper.UserCoinMapper;
import com.dubbo.lian.cms.modules.app.service.ISysMissionService;
import com.dubbo.lian.cms.modules.app.SysMission;
import com.dubbo.lian.cms.modules.app.UserCoin;
import com.dubbo.lian.cms.modules.app.UserMission;
import com.dubbo.lian.cms.modules.app.mapper.UserMissionMapper;
import com.dubbo.lian.cms.modules.app.service.IUserMissionService;
import com.dubbo.lian.common.util.BaseR;
import com.dubbo.lian.common.util.ReturnTemplateUtil;
import com.dubbo.lian.common.util.TimeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 import java.util.*;
 /**
 * <p>
 * 用户选择的任务列表  服务实现类
 * </p>
 *
 * @author lian
 * @since 2016-12-22
 */
@Service
public class UserMissionServiceImpl extends ServiceImpl<UserMissionMapper, UserMission> implements IUserMissionService {
     private static Logger logger = LoggerFactory.getLogger(UserMissionServiceImpl.class);
    @Autowired
    private ISysMissionService sysMissionService;
    @Autowired
    private UserMissionMapper userMissionMapper;
    @Autowired
    private UserCoinMapper userCoinMapper;
     @Override
    public BaseR insertUserMissionList(JSONObject json) throws Exception {
        {
             String userid = json.getString("userid");
            List missionIdList = json.getJSONArray("missionIdList");
            String targetDate = json.getString("targetDate");
            int planStage = json.getIntValue("planStage");
             EntityWrapper sysPlanSelectEntity = new EntityWrapper();
            sysPlanSelectEntity.in("mission_id",missionIdList);
            List<SysMission> missionsList = sysMissionService.selectList(sysPlanSelectEntity);
             String todayDate = TimeUtil.getDate();
             if(TimeUtil.contrastStringDate(todayDate,targetDate)<0){
                throw new Exception("这目标时间有问题");
            }
             long total_day = TimeUtil.getInterval(todayDate,targetDate);
             List<UserMission> userMissionList = new ArrayList<UserMission>();
            for (int i = 0; i <= total_day; i++) {
                String date = TimeUtil.GetSysDate("yyyy-MM-dd",todayDate,0,0,i);
                 for (SysMission sysMission : missionsList) {
                    UserMission userMission = new UserMission();
                    userMission.setUserid(userid);
                    userMission.setHongmengCoin(sysMission.getHongmengCoin());
                    userMission.setMissionId(sysMission.getMissionId());
                    userMission.setPlanStage(planStage);
                    userMission.setMissionName(sysMission.getMissionName());
                    userMission.setTime(TimeUtil.parse(date));
                    userMission.setIsDel(0);
                    userMission.setIsComplete(0);
                    userMissionList.add(userMission);
                    System.out.println(userMission.toString());
                }
            }
            try {
                 super.insertBatch(userMissionList,userMissionList.size());
            } catch (Exception e) {
                throw new Exception(e.getMessage());
            }
             return null;
        }
     }
     @Override
    public BaseR selectUserMissionList(JSONObject json) {
        String userid = json.getString("userid");
        Integer planStage = json.getInteger("planStage");
        String startTime = json.getString("startTime");
        String endTime = json.getString("endTime");
        Integer isDesc = json.getInteger("isDesc");
        Integer pageNum = json.getInteger("pageNum");
        Integer pageSize = json.getInteger("pageSize");
        Integer isComplete = json.getInteger("isComplete");
        Integer isDel = json.getInteger("isDel");
        if(pageNum==null){
            pageNum=1;
        }
        if(pageSize==null){
            pageSize=200;
        }
        if(isDel==null){
            isDel=0;
        }
        Page<UserMission> page = new Page<>(pageNum,pageSize);
        Map selectMap = new HashMap();
        selectMap.put("userid",userid);
        selectMap.put("planStage",planStage);
        selectMap.put("startTime",startTime);
        selectMap.put("endTime",endTime);
        selectMap.put("isDesc",isDesc);
        selectMap.put("isComplete",isComplete);
        selectMap.put("isDel",isDel);
        List<UserMission> userMissionList = userMissionMapper.selectUserMission(selectMap,page);
        Map returnMap = new HashMap();
        returnMap.put("missionList",userMissionList);
        return ReturnTemplateUtil.returnOfObjectTime(returnMap);
    }
     @Override
    public BaseR selectCoinSum(UserMission obj) {
        int coinSum = 0;
        try {
            coinSum = userMissionMapper.selectCoin(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Map coinMap = new HashMap();
        coinMap.put("hongmengCoin",coinSum);
        return ReturnTemplateUtil.returnOfMap(coinMap);
    }
     @Override
    public BaseR updateUserMissionComplete(JSONObject json) throws Exception {
        int userMissionId = json.getIntValue("userMissionId");
        String userid = json.getString("userid");
        UserMission userMission = super.selectById(userMissionId);
        if(userMission==null){
            throw new Exception("没有找到该任务");
        }
        if(userMission.getIsComplete()==1){
            throw new Exception("任务已完成");
        }
         UserMission missionParam = new UserMission();
        missionParam.setCompleteTime(new Date());
        missionParam.setIsComplete(1);
        missionParam.setUserMissionId(userMissionId);
         boolean MissionUpdateBoolean = super.updateById(missionParam);
        if (!MissionUpdateBoolean) {
            throw new Exception("更新失败");
        }
         UserMission userMissionCoin = super.selectById(missionParam);
        int missionCoin = userMissionCoin.getHongmengCoin();
         Map userCoinParam = new HashMap();
        userCoinParam.put("userid",userid);
        Integer userCoin = userCoinMapper.selectUserCoin(userCoinParam);
         if (userCoin==null){
            UserCoin insertParam = new UserCoin();
            insertParam.setUserid(userid);
            insertParam.setHongmengCoin(missionCoin);
            userCoinMapper.insert(insertParam);
        }else {
            UserCoin updateParam = new UserCoin();
            updateParam.setUserid(userid);
            updateParam.setHongmengCoin(missionCoin+userCoin);
            userCoinMapper.updateById(updateParam);
        }
        return ReturnTemplateUtil.returnOfNull();
    }
     @Override
    public BaseR deleteUserMission(JSONObject json) {
        String userid = json.getString("userid");
        try {
            deleteUserMissionListMethod(userid);
        } catch (Exception e) {
            return ReturnTemplateUtil.errReturn(e.getMessage(),logger);
        }
        return ReturnTemplateUtil.returnOfNull();
    }
     void deleteUserMissionListMethod(String userid) throws Exception{
        UserMission userMission = new UserMission();
        userMission.setIsDel(1);
        userMission.update("userid={0}",userid);
    }
     @Override
    public BaseR updateUserMissionList(JSONObject json) throws Exception{
        {
             String userid = json.getString("userid");
            List missionIdList = json.getJSONArray("missionIdList");
            int planStage = json.getIntValue("planStage");
             EntityWrapper sysPlanSelectEntity = new EntityWrapper();
            sysPlanSelectEntity.in("mission_id",missionIdList);
            List<SysMission> missionsList = sysMissionService.selectList(sysPlanSelectEntity);
             String todayDate = TimeUtil.getDate();
            UserTarget userTarget= new UserTarget();
            UserTarget userTarget1 = userTarget.selectById(userid);
            Date targetDate = userTarget1.getTargetDate();
            String targetDateStr = TimeUtil.format(targetDate,"yyyy-MM-dd");
             if(TimeUtil.contrastStringDate(todayDate,targetDateStr)<0){
                throw new Exception("这目标时间有问题");
            }
             long total_day = 0;
            try {
                total_day = TimeUtil.getInterval(todayDate,targetDateStr);
            } catch (Exception e) {
                e.printStackTrace();
                throw new Exception(e);
            }
             List<UserMission> userMissionList = new ArrayList<UserMission>();
            for (int i = 0; i <= total_day; i++) {
                String date = TimeUtil.GetSysDate("yyyy-MM-dd",todayDate,0,0,i);
                 for (SysMission sysMission : missionsList) {
                    UserMission userMission = new UserMission();
                    userMission.setUserid(userid);
                    userMission.setHongmengCoin(sysMission.getHongmengCoin());
                    userMission.setMissionId(sysMission.getMissionId());
                    userMission.setPlanStage(planStage);
                    userMission.setMissionName(sysMission.getMissionName());
                    userMission.setTime(TimeUtil.parse(date));
                    userMission.setIsDel(0);
                    userMission.setIsComplete(0);
                    userMissionList.add(userMission);
                }
            }
            try {
                deleteUserMissionListMethod(userid);
                super.insertBatch(userMissionList,userMissionList.size());
            } catch (Exception e) {
                System.out.println(e);
                throw new Exception(e.getMessage());
            }
             return null;
        }
     }
     @Override
    public BaseR selectUserMissionIsThere(JSONObject obj) {
        Map selectMap = new HashMap();
        Map returnMap = new HashMap();
        try {
            String userid = obj.getString("userid");
            selectMap.put("userid",userid);
            int count = userMissionMapper.selectUserMissionIsThere(selectMap);
            if (count==0){
                returnMap.put("isThere",false);
            }else{
                returnMap.put("isThere",true);
            }
        } catch (Exception e) {
            ReturnTemplateUtil.errReturn(e.getMessage(),logger);
        }
        return ReturnTemplateUtil.returnOfMap(returnMap);
    }
     @Override
    public BaseR selectSysmissionStageCoin(JSONObject json) {
         return null;
    }
 }